define(function(require,exports,module){
	exports.init = function(){

	}	
	exports.operate = function(){
		//清空购物车
		exports.clean_my_car();
		//加数量
		add_food_num();
		//减数量
		plus_food_num();
		//输入数字的时候
		input_food_num();

		//点击去结算的时候，检查是否登录
		$(".pay-for-car").click(function(){
			$.post(my_car_var.checkUserLoginUrl,{},function(e){
				if(!e.status){
					$('#loginModal').modal()
				}else{
					location.href = my_car_var.createOrderUrl;
				}
			},'json');
		})
	}
	exports.win_resize = function(){

	}
	exports.clean_my_car = function(){
		$(".clean_my_car_operate").live('click',function(){
			$.post(my_car_var.cleanMyCarUrl,{},function(e){
				$.Zebra_Dialog(e.message, {
				    'type':'information',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    	location.reload();
				    }
				});

			},'json');
		})
	}

	function add_food_num(){
		$(".mycar-add").live('click',function(){
			var food_num = $(this).prev().val();
			var food_id = $(this).parents("tr").attr("data-id");
			var add_num = parseInt(food_num) + 1;
			$(this).prev().val(add_num);
			var data = {
				food_id:food_id,
				food_num:add_num
			};
			$.post(my_car_var.changeMyCarNumUrl,data,function(e){
				$(".goods_total_price").html("+¥"+e.data.goodsTotalPrice);
				$(".goods_differ").html("-<font class='show_important_red'>¥"+e.data.goodsDiffer+"</font>");
				$(".goods_total_uprice").html("<font class='show_important_red'>¥"+e.data.goodsTotalUPrice+"</font>");
			},'json');
		})
	}
	function plus_food_num(){
		$(".mycar-plus").live("click",function(){
			var food_num = $(this).next().val();
			var food_id = $(this).parents("tr").attr("data-id");
			if(food_num <= 1){
				var data = {
					food_id:food_id,
					food_num:1					
				}
				$.post(my_car_var.changeMyCarNumUrl,data,function(e){
					$(".goods_total_price").html("+¥"+e.data.goodsTotalPrice);
					$(".goods_differ").html("-<font class='show_important_red'>¥"+e.data.goodsDiffer+"</font>");
					$(".goods_total_uprice").html("<font class='show_important_red'>¥"+e.data.goodsTotalUPrice+"</font>");
				},'json');				
				alert("已是最小");
				return;
			}
			var add_num = parseInt(food_num) - 1;
			$(this).next().val(add_num);
			var data = {
				food_id:food_id,
				food_num:add_num
			};
			$.post(my_car_var.changeMyCarNumUrl,data,function(e){
				$(".goods_total_price").html("+¥"+e.data.goodsTotalPrice);
				$(".goods_differ").html("-<font class='show_important_red'>¥"+e.data.goodsDiffer+"</font>");
				$(".goods_total_uprice").html("<font class='show_important_red'>¥"+e.data.goodsTotalUPrice+"</font>");
			},'json');			
		})
	}
	function input_food_num(){
		$(".mycar-input").live('input',function(){
			var value = $(this).val().trim();
			var num_reg = /^[0-9]+$/;
			if(value == '' ){
				alert("请正确输入数字");
				return;
			}
			if(!num_reg.test(value)){
				alert("请输入正确的数字");
				return;
			}
			var food_id = $(this).parents("tr").attr("data-id");
			var data = {
				food_id:food_id,
				food_num:value
			};
			$.post(my_car_var.changeMyCarNumUrl,data,function(e){
				$(".goods_total_price").html("+¥"+e.data.goodsTotalPrice);
				$(".goods_differ").html("-<font class='show_important_red'>¥"+e.data.goodsDiffer+"</font>");
				$(".goods_total_uprice").html("<font class='show_important_red'>¥"+e.data.goodsTotalUPrice+"</font>");
			},'json');				
		})
	}
});